//
//  MvPlayViewController.m
//  CatMusic
//
//  Created by dllo on 15/4/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MvPlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MvPlayViewController ()<MPMediaPickerControllerDelegate, UIAccelerometerDelegate>

@property(nonatomic, retain)MPMoviePlayerViewController *mvPlayerViewControl;

@end

@implementation MvPlayViewController

-(void)dealloc
{
    [_arr release];
    [_mvPlayerViewControl release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self playNativeMoive];
    NSLog(@"%@", self.url);
    [self showMessage:@"双击全屏"];
//    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES];
    
    
//    self.navigationController.navigationBar.hidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
    
//    //设置应用程序的状态栏到指定的方向
//    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
//    //view旋转
//    [self.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];
////    [[UIApplication sharedApplication] setStatusBarHidden:YES];
//    
//    // 隐藏状态栏
//    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
//        // iOS 7
//        [self prefersStatusBarHidden];
//        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
//    }
    
}

// 提示框
- (void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    showview.frame = CGRectMake(WIDTH / 2 - 30, HEIGHT / 3, 100, 100);
    [UIView animateWithDuration:2 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}


- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}

-(void)viewWillAppear:(BOOL)animated
{
    //隐藏navigationController
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;

    //隐藏状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];}

- (void)viewWillDisappear:(BOOL)animated
{
    //显示状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    //显示navigationController
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}



// 视频播放页面
- (void)playNativeMoive
{
    NSString *str = self.url;
    NSURL *url = [NSURL URLWithString:str];
    self.mvPlayerViewControl = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:[self.mvPlayerViewControl moviePlayer]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeMovie:) name:MPMoviePlayerScalingModeDidChangeNotification object:[self.mvPlayerViewControl moviePlayer]];
    [self.view addSubview:self.mvPlayerViewControl.view];
    MPMoviePlayerController *player = [self.mvPlayerViewControl moviePlayer];
    [player play];
    
}

- (void)changeMovie:(NSNotification *)theNotification
{
   
    CGAffineTransform l = CGAffineTransformMakeRotation(M_PI_2);
    self.mvPlayerViewControl.view.transform = l;
    [self.mvPlayerViewControl.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //    self.mvPlayer.view.frame = self.view.frame;


}

- (void)playMovieFinished:(NSNotification *)theNotification
{
    MPMoviePlayerController *mvPlayer = [theNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mvPlayer];
    [mvPlayer stop];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
